//
//  NSString+Base.m
//  DatePicker
//
//  Created by Burn on 2018/11/13.
//  Copyright © 2018 nmgxysh. All rights reserved.
//

#import "NSString+Base.h"

@implementation NSString (Base)

//手机号码验证
/*
 130~139  145,147 15[012356789] 180~189
 */
+ (BOOL)validateMobile:(NSString *)mobile
{
    //手机号以13， 15，18开头，八个 \d 数字字符
    NSString *phoneRegex = @"^(0|86|17951)?(13[0-9]|15[012356789]|18[0-9]|14[57])[0-9]{8}$";
    NSPredicate *phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",phoneRegex];
    return [phoneTest evaluateWithObject:mobile];
}




/**判断字符串中是否包含符号*/
- (BOOL)judgeTheillegalCharacter:(NSString *)content{
    //提示 标签不能输入特殊字符
    NSString *str =@"^[A-Za-z0-9\\u4e00-\u9fa5]+$";
    NSPredicate* emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", str];
    if (![emailTest evaluateWithObject:content]) {
        return YES;
    }
    return NO;
}

/**字符串是否都是数字*/
+ (BOOL)isPureInt:(NSString *)string{
    
    NSScanner* scan = [NSScanner scannerWithString:string];
    
    int val;
    
    return [scan scanInt:&val] && [scan isAtEnd];
    
}




/**获取当前时间 分钟 如果45 分钟 返回 40*/
+ (NSUInteger )getCurrentMinute{
    NSString *time = [self getCurrentTimes];
    // 拆分
    NSArray *timeArray = [time componentsSeparatedByString:@" "];
    //  HH : mm : ss
    NSString *ymd = timeArray.lastObject;
    //  拆分
    NSArray *ymdArray = [ymd componentsSeparatedByString:@":"];
    
    
    NSString *m = ymdArray[1];
    NSString *substring = [m substringToIndex:1];
    NSString *mStr = [substring stringByAppendingString:@"0"];
    
    
    return [mStr integerValue];
}


/**获取周天*/
+ (NSInteger)getWeekday:(NSDate *)date {
    /*1 = 周日 2 = 周一 .... 7 = 周六*/
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSDateComponents *comps = [[NSDateComponents alloc] init];
    NSInteger unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitWeekday |
    NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
    // 话说在真机上需要设置区域，才能正确获取本地日期，天朝代码:zh_CN
    calendar.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];
    comps = [calendar components:unitFlags fromDate:date];
    return [comps weekday];
}

/**获取周天这个是返回正常1234567*/
+ (NSInteger)getWeekDay:(NSDate *)date{
    NSInteger value = [self getWeekday:date];
    switch (value) {
        case 1:
            return 7;
        case 2:
            return 1;
        case 3:
            return 2;
        case 4:
            return 3;
        case 5:
            return 4;
        case 6:
            return 5;
        case 7:
            return 6;
            break;
    }
    return 0;
}



/**获取当前时间 点数*/
+ (NSUInteger )getCurrentHour{
    
    NSString *time = [self getCurrentTimes];
    // 拆分
    NSArray *timeArray = [time componentsSeparatedByString:@" "];
    //  HH : mm : ss
    NSString *ymd = timeArray.lastObject;
    //  拆分
    NSArray *ymdArray = [ymd componentsSeparatedByString:@":"];
    
    return [ymdArray.firstObject integerValue];
}

/**获取月日周*/
+ (NSString*)monthDayWeeks:(NSDate *)date isToday:(BOOL)today{
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"MM-dd-EEEE"];

    NSString *currentTimeString = [formatter stringFromDate:date];
    
    NSArray *dateArray = [currentTimeString componentsSeparatedByString:@"-"];
    
    NSMutableString *returnStr = [[NSMutableString alloc]initWithString:@""];
    [returnStr appendString:dateArray[0]];
    [returnStr appendString:@"月"];
    [returnStr appendString:dateArray[1]];
    [returnStr appendString:@"日"];
    
    if (today) {
        [returnStr appendString:[NSString stringWithFormat:@" 今天"]];
    }else{
        [returnStr appendString:[NSString stringWithFormat:@" %@",dateArray.lastObject]];
    }

    return returnStr;
    
}


+ (NSString*)getCurrentTimes{
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    
    // ----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制
    [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
    
    //现在时间,你可以输出来看下是什么格式
    NSDate *datenow = [NSDate date];
    
    //----------将nsdate按formatter格式转成nsstring
    NSString *currentTimeString = [formatter stringFromDate:datenow];
    
    
    return currentTimeString;
    
}


/**
 本月一共有多少天
 
 @return 天
 */
+ (NSUInteger)currenttMonthDayNum{
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSRange range = [calendar rangeOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitMonth forDate:[NSDate date]];
    NSUInteger numberOfDaysInMonth = range.length;
    return numberOfDaysInMonth;
}

/**该月有多少天*/
+ (NSUInteger )monthDayNum:(NSDate *)date{
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSRange range = [calendar rangeOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitMonth forDate:date];
    NSUInteger numberOfDaysInMonth = range.length;
    return numberOfDaysInMonth;
}

/**当前月剩余多少天*/
+ (NSUInteger)currentMonthBeLeftDay{
    //  获取当前时间
    NSString *time = [self getCurrentTimes];
    // 拆分
    NSArray *timeArray = [time componentsSeparatedByString:@" "];
    //  年月日
    NSString *ymd = timeArray.firstObject;
    //  拆分
    NSArray *ymdArray = [ymd componentsSeparatedByString:@"-"];
    //  取出天数
    NSString *dayStr = ymdArray.lastObject;
    //  转int
    NSInteger day = [dayStr integerValue];
    //  当月用有多少天
    NSUInteger days = [self currenttMonthDayNum];
    
    return days - day;
}


/**当年剩余多少月*/
+ (NSUInteger)currentYearBeLeftMonth{
    //  获取当前时间
    NSString *time = [self getCurrentTimes];
    // 拆分
    NSArray *timeArray = [time componentsSeparatedByString:@" "];
    //  年月日
    NSString *ymd = timeArray.firstObject;
    //  拆分
    NSArray *ymdArray = [ymd componentsSeparatedByString:@"-"];
    //  月
    return 12 - [ymdArray[1] integerValue];
}

/**今年已经过去多少天了(不包含今天)*/
+ (NSUInteger)howManyDaysInCurrentYearFuture{
    
    //  获取当前时间
    NSString *time = [self getCurrentTimes];
    // 拆分
    NSArray *timeArray = [time componentsSeparatedByString:@" "];
    //  年月日
    NSString *ymd = timeArray.firstObject;
    //  拆分
    NSArray *ymdArray = [ymd componentsSeparatedByString:@"-"];
    //  取出月数
    NSUInteger month = [ymdArray[1]integerValue];
    //  取出天数
    NSString *dayStr = ymdArray.lastObject;
    //  转int    今天是几号
    NSInteger day = [dayStr integerValue];
    
    if (month > 1) {
        NSInteger days = 0;
        for (int i = 1; i <= month - 1; i++) {
            days += [self howManyDaysInThisMonth:i];
        }
        return days + day - 1;
    }else{
        return  day - 1;
    }
}

/**今年剩余多少天*/
+ (NSUInteger)currentYearBeLeftHowManyDay{
    NSUInteger allDays = 0;
    for (int i = 1; i <= 12; i++) {
        allDays += [self howManyDaysInThisMonth:i];
    }
    NSUInteger future = [self howManyDaysInCurrentYearFuture];
    return (long long)allDays - future;
}






+ (NSInteger)howManyDaysInThisMonth :(NSInteger)imonth {
    
    int year = [[self years][0] intValue];
    
    if((imonth == 1)||(imonth == 3)||(imonth == 5)||(imonth == 7)||(imonth == 8)||(imonth == 10)||(imonth == 12))
        return 31 ;
    
    if((imonth == 4)||(imonth == 6)||(imonth == 9)||(imonth == 11))
        return 30;
    
    if ((year%4 == 1)||(year%4 == 2)||(year%4 == 3))
    {
        return 28;
    }
    
    if(year%400 == 0)
        return 29;
    
    if(year%100 == 0)
        
        return 28;
    
    return 29;
    
}


#pragma mark 获取当前的年月
+ (NSArray *) years {
    
    NSMutableArray *yearArr = [[NSMutableArray alloc]init];
    
    NSDate *now = [NSDate date];
    
    NSCalendar *calendar = [NSCalendar currentCalendar];
    
    NSUInteger unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth;
    
    NSDateComponents *dateComponent = [calendar components:unitFlags fromDate:now];
    
    NSInteger year = [dateComponent year];
    
    NSInteger month = [dateComponent month];
    
    [yearArr addObject:@(year)];
    
    [yearArr addObject:@(month)];
    
    return yearArr;
    
}



@end
